<template>
<h1>员工列表综合练习</h1>
  <el-form style="width: 500px;margin: 0 auto;" v-model="emp">
    <el-form-item label="姓名">
      <el-input v-model="emp.uname"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input v-model="emp.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input v-model="emp.job"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="add">提交</el-button>
    </el-form-item>
  </el-form>
<!--  <el-button type="primary" @click="add">提交</el-button>-->
  <el-table :data="arr" style="width: 600px;margin: 0 auto;" border="1px">
    <el-table-column label="编号" type="index" width="88" align="center"></el-table-column>
    <el-table-column label="姓名" prop="uname"></el-table-column>
    <el-table-column label="薪资" prop="salary"></el-table-column>
    <el-table-column label="职位" prop="job"></el-table-column>
    <el-table-column label="操作">
      <!--自定义列模板template-->
      <!--      #default 定义一个插槽(slot)名为default
      scope就是一个对象,该对象是一个范围对象,可以保存上下文数据-->
      <template #default="scope">
        <!--        scoped对象保存了当前行数据的索引$index(从0开始)与当前的完整对象scope.row-->
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";
const emp = ref({uname:'',salary:'',job:''})
const arr = ref([])
function add(){
  if(emp.value.uname.trim()=='' || emp.value.salary.trim()=='' || emp.value.job.trim()==''){
    alert('请输入完整数据!');
    return;
  }
  arr.value.push(emp.value)
  emp.value = {}
}
function del(index,row){
  if (confirm('您确认要删除' + row.uname + '吗?')){
    arr.value.splice(index,1);
    ElMessage.success(row.uname+'已成功删除');
  }
}

</script>

<style scoped>

</style>